#include "dowconstraintwindow.h"
#include "../dowconstraint.h"
#include <boost/shared_ptr.hpp>
#include "../logger.h"

DOWConstraintWindow::DOWConstraintWindow(BaseObjectType* cobject,
                                         const Glib::RefPtr<Gnome::Glade::Xml>& ui_xml)
	: Gtk::Dialog(cobject),
	  ok_button_(0),
	  cancel_button_(0),
      days_combobox_(0)
{
	// connect a handler to the delete-event signal, which is sent when the
	// user closes the window
	signal_delete_event().connect(
		sigc::mem_fun(*this, &DOWConstraintWindow::on_delete_event));

	// initialize and set up child widgets
	ui_xml->get_widget("cancel_button", cancel_button_);
	cancel_button_->signal_clicked().connect(
		sigc::mem_fun(*this, &DOWConstraintWindow::on_cancel));
	ui_xml->get_widget("ok_button", ok_button_);
	ok_button_->signal_clicked().connect(
		sigc::mem_fun(*this, &DOWConstraintWindow::on_ok));
	ui_xml->get_widget("days_combobox", days_combobox_);
}

DOWConstraintWindow::~DOWConstraintWindow()
{
}

bool DOWConstraintWindow::on_delete_event(GdkEventAny*)
{
	on_cancel();
    return true;
}

void DOWConstraintWindow::on_ok()
{
    boost::shared_ptr<Constraint> dow_constraint;
    int selected_row = days_combobox_->get_active_row_number();
    switch( selected_row ) {
    case -1:
        on_cancel();
        return;
    case 0:
        dow_constraint.reset(new DOWConstraint(DOWConstraint::Monday));
        constraint(dow_constraint);
        break;
    case 1:
        dow_constraint.reset(new DOWConstraint(DOWConstraint::Tuesday));
        constraint(dow_constraint);
        break;
    case 2:
        dow_constraint.reset(new DOWConstraint(DOWConstraint::Wednesday));
        constraint(dow_constraint);
        break;
    case 3:
        dow_constraint.reset(new DOWConstraint(DOWConstraint::Thursday));
        constraint(dow_constraint);
        break;
    case 4:
        dow_constraint.reset(new DOWConstraint(DOWConstraint::Friday));
        constraint(dow_constraint);
        break;
    case 5:
        dow_constraint.reset(new DOWConstraint(DOWConstraint::Saturday));
        constraint(dow_constraint);
        break;
    case 6:
        dow_constraint.reset(new DOWConstraint(DOWConstraint::Sunday));
        constraint(dow_constraint);
        break;
    }
	hide();
	response(Gtk::RESPONSE_ACCEPT);
}

void DOWConstraintWindow::on_cancel()
{
    Logger::instance()->message("Cancelling-out Day-of-Week Window.", 3);
	hide();
	response(Gtk::RESPONSE_REJECT);
}

boost::shared_ptr<Constraint> DOWConstraintWindow::constraint() const
{
    return constraint_;
}

void DOWConstraintWindow::constraint(boost::shared_ptr<Constraint> constraint)
{
    constraint_ = constraint;
}
